var dtClass = null;
var dataTable = null;
var re = null;
layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['util', 'lay', 'table'], function () {
        var $ = layui.$;
        var table = layui.table;
        dataTable = layui.table;
        var util = layui.util;

        table.render({
            elem: '#dictClass',
            height: 'full-65',
            url: './getDictionaryClassData?model=1',
            cellMinWidth: 80,
            skin: 'nob',
            cols: [
                [
                    {field: 'name', title: '词典名称'}
                ]
            ],
            done: function (res, curr, count) {
                //表格加载完成后，默认选中第一行
                $('.layui-table-view[lay-table-id="dictClass"]').children('.layui-table-box').children('.layui-table-body').find('table tbody tr[data-index="0"]').click();
            },
        });

        //监听行单击事件
        table.on('row(dictClass)', function (obj) {
            var data = obj.data;
            dtClass = data;
            getListData(data);
            //标注选中样式
            obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");
        });

        function getListData(options) {
            Title = document.getElementById('dict-title');
            Title.innerText = '【' + options.name + '】 字典信息';
            re = dataTable.render({
                elem: '#dataList'
                , url: './getDictionaryData?classId=' + options.id
                , cellMinWidth: 80
                , page: false
                , limit: 50
                , height: 'full-120'
                , loading: true
                , size: 'sm'
                , cols: [
                    [ //标题栏
                        {field: 'name', title: '名称', width: 390}
                        , {field: 'code', title: '值', width: 150}
                        , {field: 'remark', title: '备注'}
                        , {fixed: 'right', title: '操作', width: 130, toolbar: '#operateBar'}
                    ]
                ]
            });

        }

        //监听工具条
        dataTable.on('tool(dataList)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case "edit":
                    top.layer.open({
                        type: 2,
                        title: '编辑' + data.name + '信息',
                        content: './basic/setDictData?' + 'id=' + data.id + '&classId=' + dtClass.id,
                        area: ['600px', '390px'],
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Buttun_Submit()) {
                                dataTable.reload('dataList');
                            }
                        },
                        btn2: function (index) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    break;
                case "del":
                    parent.layer.confirm('真的删除 ' + data.name + ' 吗?', function (index) {
                        $.ajax({
                            url: './delDataDict', //url
                            contentType: "application/json",
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: JSON.stringify({id: data.id}),
                            success: function (result) {
                                if (result.code == 0 || result.code == 200) {
                                    obj.del();
                                    parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                    });
            }
        });

        util.on("lay-event", {
            newDataDict: function () {
                top.layer.open({
                    type: 2,
                    title: '新增' + dtClass.name + '信息',
                    content: './basic/setDictData?' + 'id=&classId=' + dtClass.id,
                    area: ['600px', '390px'],
                    shadeClose: false,
                    resize: false,
                    btn: [
                        '<i class="layui-icon">&#xe605;</i> 确定',
                        '<i class="layui-icon">&#x1006;</i> 取消',
                    ],
                    yes: function (index, layero) {
                        var iframeWin = layero.find('iframe')[0];
                        if (iframeWin.contentWindow.Buttun_Submit()) {
                            dataTable.reload('dataList');
                        }
                    },
                    btn2: function (index) {
                        layer.close(index); //如果设定了yes回调，需进行手工关闭
                    },
                });
            },
        });
    });
